
package controllers;

import play.data.*;
import play.mvc.*;
import models.*;
import views.html.categorie_ditte.*;

public class Categorie_Ditte extends Controller {
	
	static Form<Categoria_Ditta> categoria_dittaForm = Form.form(Categoria_Ditta.class);
	
	//SHOW
	  
	public static Result categorie_ditte() {
		return ok(categoria_ditta.render(Categoria_Ditta.all(), categoria_dittaForm));
	}
	
	//NEW
	@Security.Authenticated(SecuredWriter.class)
	public static Result newCategoria_Ditta() {
		Form<Categoria_Ditta> filledForm = categoria_dittaForm.bindFromRequest();
		  if(filledForm.hasErrors()) {
			  return badRequest(nuova_categoria_ditta.render(filledForm,Categoria.all(),Classifica.all()));
		  } 
		  else {
			  Categoria_Ditta.create(filledForm.get());
			  
			  return redirect(routes.Ditte.findDitta());  
		  }
	}
	
	//DELETE
	@Security.Authenticated(SecuredWriter.class)
	public static Result deleteCategoria_Ditta(Long id) {
			Categoria_Ditta.delete(id);
	   		return redirect(routes.Ditte.findDitta());
	}

	
	//EDIT
	
	@Security.Authenticated(SecuredWriter.class)
		public static Result editCategoria_Ditta(Long id) {

			
			Categoria_Ditta daModificare=Categoria_Ditta.find.byId(id);
			
			Categoria_Ditta tmp= new Categoria_Ditta();
			
		    tmp.id=daModificare.id;
		    tmp.ditta=daModificare.ditta;
		    tmp.categoria=daModificare.categoria;
		    tmp.classifica=daModificare.classifica;
		    tmp.protocollo=daModificare.protocollo;    

		    
		    Form<Categoria_Ditta> filledForm = Form.form(Categoria_Ditta.class).bindFromRequest();
		    
			if(filledForm.hasErrors()) {
					return badRequest(edit_categoria_ditta.render(Categoria_Ditta.all(), filledForm.fill(tmp),id,daModificare));
			}
		   	else{
		   			tmp.id=id;
		   			tmp.ditta=filledForm.get().ditta;
		   			tmp.categoria=filledForm.get().categoria;
		   			tmp.classifica=filledForm.get().classifica;
		   			tmp.protocollo=filledForm.get().protocollo;
		    		
		   			Categoria_Ditta.update(tmp);
		    		
					return redirect(routes.Ditte.findDitta()); 
			} 
			
		}

}